<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>电子围栏设置</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <script src="http://webapi.amap.com/maps?v=<?php echo (C("Amap_Version")); ?>&key=<?php echo (C("Amap_Web_Key")); ?>"></script>

	<script type="text/javascript" src="/electriccar/Public/admin/js/jquery.js"></script>
	<script type="text/javascript" src="/electriccar/Public/layer/layer.js"></script>
    <style type="text/css">
        html,
        body {
            height: 100%;
            margin: 0px;
            padding: 0px
        }
        #container {
            width: 100%;
            height: 100%
        }
    </style>
</head>

<body>
	<div style="margin-bottom:5px;">
		<button onclick="editclose();" style="float:right;cursor: pointer;border: 1px solid #999;padding: 5px 10px;margin: 0 10px 10px 0;">获取坐标并确认回填</button>
		<button onclick="drawstart();" style="float:right;cursor: pointer;border: 1px solid #999;padding: 5px 10px;margin: 0 10px 10px 0;">重新绘制</button>
		<button onclick="editopen();" style="float:right;cursor: pointer;border: 1px solid #999;padding: 5px 10px;margin: 0 10px 10px 0;">开始编辑</button>
	</div>
    <div id="container"></div>
    <script type="text/javascript">
		var index = parent.layer.getFrameIndex(window.name); //获取窗口索引

		var mapObj = new AMap.Map("container", {
			resizeEnable: true,
			center: [108.947039, 34.259557],//默认钟楼
			zoom: 11
		});

		var editorTool,mousetool;

		var polygonArr = new Array();//多边形覆盖物节点坐标数组
		<?php if(is_array($set_value)): $i = 0; $__LIST__ = $set_value;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$value): $mod = ($i % 2 );++$i;?>polygonArr.push([<?php echo ($value); ?>]);<?php endforeach; endif; else: echo "" ;endif; ?>

		var polygon = new AMap.Polygon({
			map: mapObj,
			path: polygonArr,
			strokeColor: "#F33", //线颜色
            strokeOpacity: 1, //线透明度
            strokeWeight: 3, //线粗细度
            fillColor: "#ee2200", //填充颜色
            fillOpacity: 0.35//填充透明度
		}); 
		//添加编辑控件
		mapObj.plugin(["AMap.PolyEditor"], function() {
			editorTool = new AMap.PolyEditor(mapObj, polygon);		
		});

		//重新绘制多边形
		function drawstart(){
			mapObj.clearMap();
			var polygonOption = {
				strokeColor: "#F33", //线颜色
				strokeOpacity: 1, //线透明度
				strokeWeight: 3, //线粗细度
				fillColor: "#ee2200", //填充颜色
				fillOpacity: 0.35//填充透明度
			}; 
			mapObj.plugin(["AMap.MouseTool"],function(){ 
				mousetool = new AMap.MouseTool(mapObj); 
				mousetool.polygon(polygonOption); //使用鼠标工具，在地图上画标记点
				//添加监听
				AMap.event.addListener(mousetool,"draw",function callback(e){
					var drawObj = e.obj;  //obj属性就是绘制完成的覆盖物对象。
					var pointsCount = drawObj.getPath().length; //获取节点个数
					var path=drawObj.getPath();
					var pathinfo="";
					$.each(path,function(key,value){  
						if(pathinfo.length==0){
							//pathinfo=value.lng+","+value.lat;
							pathinfo=value.I+","+value.L;
						}else{
							//pathinfo+="|"+value.lng+","+value.lat;
							pathinfo+="|"+value.I+","+value.L;
						}
					})
					parent.$('#set_value').val(pathinfo);//将坐标值回传
					parent.layer.close(index);	
				});
			});
		}
		//开始编辑
		function editopen(){
			editorTool.open();
		}
		//结束编辑
		function editclose(){
			editorTool.close();
			var pointsCount = polygon.getPath().length; //获取节点个数
			//console.log("多边形节点数："+pointsCount+"<br>节点坐标："+polygon.getPath());
			var path=polygon.getPath();
			//console.log(path);
			var pathinfo="";
			$.each(path,function(key,value){  
				if(pathinfo.length==0){
					//pathinfo=value.lng+","+value.lat;
					pathinfo=value.I+","+value.L;
				}else{
					//pathinfo+="|"+value.lng+","+value.lat;
					pathinfo+="|"+value.I+","+value.L;
				}
			})
			parent.$('#set_value').val(pathinfo);//将坐标值回传
			parent.layer.close(index);			
		}
		
    </script>
</body>

</html>